var mongoose = require('mongoose');
const Comment = require('../model/comment.model'); // 数据模型
// 增
exports.create = function(req, res, next) {
	const comment = new Comment(req.body);
	comment.save().then(data => {
		res.json(data);
	});
};
// // 删
exports.remove = function(req,res,next){
    var id = req.params.id;
    Comment.findByIdAndRemove(id,function(err,data){
        res.json({message:'success done'});
    })
}

// // 改 req.body,req.query,req.params
// exports.update = function(req, res, next) {
// 	const id = req.params.id;
// 	Comment.findByIdAndUpdate(id, {
// 		$set: req.body
// 	}, {
// 		new: false
// 	}).then(data => {
// 		res.json(data);
// 	});
// };

// // 查
exports.list = function(req, res, next) {
	var page = req.body.page ? req.body.page : 1;
	var rows = req.body.rows ? req.body.rows : 5;
	var queryCondition = {};

	if(req.body.desc && req.body.desc.trim().length > 0) {
		desc = req.body.desc;

		queryCondition = {
			desc: new RegExp(desc, 'i')
		};
	}

	if(req.body.commentId && req.body.commentId.trim().length > 0) {
		commentId = req.body.commentId;
		queryCondition = Object.assign(queryCondition, {
			commentId: commentId
		});
	}

	Comment.paginate(queryCondition, {
		page: +page,
		limit: +rows
	}, function(
		err,
		result
	) {
		result.rows = result.docs;
		delete result.docs;
		res.json(result);
	});
};